#include "RingQueue.hpp"
using namespace std;
pthread_mutex_t p_lock;
pthread_mutex_t c_lock;
void* Productor_run(void* arg){
    sleep(1);
    RingQueue<Task>* rq = (RingQueue<Task>*)arg;
    while(true){
        int x = rand() %10;
        int y = rand() %100;
        Task t(x,y);
        pthread_mutex_lock(&p_lock);
        rq->Put(t);
        pthread_mutex_unlock(&p_lock);
        sleep(1);
        cout<<"Productor is ["<<pthread_self()<<"]"<<" Productor Task is "<<x<<" + "<<y<<" = ?"<<endl;
    }
}
void* Consumer_run(void* arg){
    sleep(2);
    RingQueue<Task>* rq = (RingQueue<Task>*)arg;
    Task t;
    while(true){
        pthread_mutex_lock(&c_lock);
        rq->Get(t);
        pthread_mutex_unlock(&c_lock);
        sleep(1);
        cout<<"Consumer is ["<<pthread_self()<<"]"<<" Consumer Task is "<<t._x<<" + "<<t._y<<" = "<<t.run()<<endl;
    }
}

int main(){
    RingQueue<Task> rq;
    pthread_t p1,p2,p3,c1,c2,c3;
    pthread_create(&p1,nullptr,Productor_run,&rq);
    pthread_create(&p2,nullptr,Productor_run,&rq);
    pthread_create(&p3,nullptr,Productor_run,&rq);
    pthread_create(&c1,nullptr,Consumer_run,&rq);
    pthread_create(&c2,nullptr,Consumer_run,&rq);
    pthread_create(&c3,nullptr,Consumer_run,&rq);

    pthread_join(p1,nullptr);
    pthread_join(p2,nullptr);
    pthread_join(p3,nullptr);
    pthread_join(c1,nullptr);
    pthread_join(c2,nullptr);
    pthread_join(c3,nullptr);

    return 0;
}
